package offer;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class offerII_0066 {
    class MapSum {

        Map<String,Integer> map;
        /** Initialize your data structure here. */
        public MapSum() {
            map=new HashMap<>();
        }

        public void insert(String key, int val) {
            map.put(key,val);
        }

        public int sum(String prefix) {
            int sum=0;
            if(map.isEmpty()) return 0;
            for(String key:map.keySet()){
                sum+=flag(prefix,key,map.get(key));
            }
            return sum;
        }

        private int flag(String pre,String key,int value){
            if(pre.length()>key.length()) return 0;
            for(int i=0;i<pre.length();++i){
                if(pre.charAt(i)!=key.charAt(i)) return 0;
            }
            return value;
        }
    }
}
